home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / Inflater.java < prev    next >
Text File  |  1998-09-22  |  8KB  |  234 lines

  1. /*
  2.  * @(#)Inflater.java    1.19 98/08/20
  3.  * 
  4.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.1_beta
  20.  * 
  21.  */
  22.  
  23. package java.util.zip;
  24.  
  25. /**
  26.  * This class provides support for general purpose decompression using
  27.  * the popular ZLIB compression library. The ZLIB compression library
  28.  * was initially developed as part of the PNG graphics standard and is
  29.  * not protected by patents. It is fully described in RFCs 1950, 1951,
  30.  * and 1952, which can be found at 
  31.  * <a href="http://info.internet.isi.edu:80/in-notes/rfc/files/">
  32.  * http://info.internet.isi.edu:80/in-notes/rfc/files/
  33.  * </a> in the files rfc1950.txt (zlib format),
  34.  * rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
  35.  * 
  36.  * @see        Deflater
  37.  * @version     1.19, 08/20/98
  38.  * @author     David Connelly
  39.  *
  40.  */
  41. public
  42. class Inflater {
  43.     private int strm;
  44.     private byte[] buf = new byte[0];
  45.     private int off, len;
  46.     private boolean finished;
  47.     private boolean needsDictionary;
  48.  
  49.     /*
  50.      * Loads the ZLIB library.
  51.      */
  52.     static {
  53.     System.loadLibrary("zip");
  54.     }
  55.  
  56.     /**
  57.      * Creates a new decompressor. If the parameter 'nowrap' is true then
  58.      * the ZLIB header and checksum fields will not be used in order to
  59.      * support the compression format used by both GZIP and PKZIP.
  60.      * @param nowrap if true then support GZIP compatible compression
  61.      */
  62.     public Inflater(boolean nowrap) {
  63.     init(nowrap);
  64.     }
  65.  
  66.     /**
  67.      * Creates a new decompressor.
  68.      */
  69.     public Inflater() {
  70.     this(false);
  71.     }
  72.  
  73.     /**
  74.      * Sets input data for decompression. Should be called whenever
  75.      * needsInput() returns true indicating that more input data is
  76.      * required.
  77.      * @param b the input data bytes
  78.      * @param off the start offset of the input data
  79.      * @param len the length of the input data
  80.      * @see Inflater#needsInput
  81.      */
  82.     public synchronized void setInput(byte[] b, int off, int len) {
  83.     if (b == null) {
  84.         throw new NullPointerException();
  85.     }
  86.     if (off < 0 || len < 0 || off + len > b.length) {
  87.         throw new ArrayIndexOutOfBoundsException();
  88.     }
  89.     this.buf = b;
  90.     this.off = off;
  91.     this.len = len;
  92.     }
  93.  
  94.     /**
  95.      * Sets input data for decompression. Should be called whenever
  96.      * needsInput() returns true indicating that more input data is
  97.      * required.
  98.      * @param b the input data bytes
  99.      * @see Inflater#needsInput
  100.      */
  101.     public void setInput(byte[] b) {
  102.     setInput(b, 0, b.length);
  103.     }
  104.  
  105.     /**
  106.      * Sets the preset dictionary to the given array of bytes. Should be
  107.      * called when inflate() returns 0 and needsDictionary() returns true
  108.      * indicating that a preset dictionary is required. The method getAdler()
  109.      * can be used to get the Adler-32 value of the dictionary needed.
  110.      * @param b the dictionary data bytes
  111.      * @param off the start offset of the data
  112.      * @param len the length of the data
  113.      * @see Inflater#needsDictionary
  114.      * @see Inflater#getAdler
  115.      */
  116.     public synchronized native void setDictionary(byte[] b, int off, int len);
  117.  
  118.     /**
  119.      * Sets the preset dictionary to the given array of bytes. Should be
  120.      * called when inflate() returns 0 and needsDictionary() returns true
  121.      * indicating that a preset dictionary is required. The method getAdler()
  122.      * can be used to get the Adler-32 value of the dictionary needed.
  123.      * @param b the dictionary data bytes
  124.      * @see Inflater#needsDictionary
  125.      * @see Inflater#getAdler
  126.      */
  127.     public void setDictionary(byte[] b) {
  128.     setDictionary(b, 0, b.length);
  129.     }
  130.  
  131.     /**
  132.      * Returns the total number of bytes remaining in the input buffer.
  133.      * This can be used to find out what bytes still remain in the input
  134.      * buffer after decompression has finished.
  135.      */
  136.     public synchronized int getRemaining() {
  137.     return len;
  138.     }
  139.  
  140.     /**
  141.      * Returns true if no data remains in the input buffer. This can
  142.      * be used to determine if #setInput should be called in order
  143.      * to provide more input.
  144.      */
  145.     public synchronized boolean needsInput() {
  146.     return len <= 0;
  147.     }
  148.  
  149.     /**
  150.      * Returns true if a preset dictionary is needed for decompression.
  151.      * @see InflatesetDictionary
  152.      */
  153.     public synchronized boolean needsDictionary() {
  154.     return needsDictionary;
  155.     }
  156.  
  157.     /**
  158.      * Return true if the end of the compressed data stream has been
  159.      * reached.
  160.      */
  161.     public synchronized boolean finished() {
  162.     return finished;
  163.     }
  164.  
  165.     /**
  166.      * Uncompresses bytes into specified buffer. Returns actual number
  167.      * of bytes uncompressed. A return value of 0 indicates that
  168.      * needsInput() or needsDictionary() should be called in order to
  169.      * determine if more input data or a preset dictionary is required.
  170.      * In the later case, getAdler() can be used to get the Adler-32
  171.      * value of the dictionary required.
  172.      * @param b the buffer for the uncompressed data
  173.      * @param off the start offset of the data
  174.      * @param len the maximum number of uncompressed bytes
  175.      * @return the actual number of uncompressed bytes
  176.      * @exception DataFormatException if the compressed data format is invalid
  177.      * @see Inflater#needsInput
  178.      * @see Inflater#needsDictionary
  179.      */
  180.     public synchronized native int inflate(byte[] b, int off, int len)
  181.         throws DataFormatException;
  182.  
  183.     /**
  184.      * Uncompresses bytes into specified buffer. Returns actual number
  185.      * of bytes uncompressed. A return value of 0 indicates that
  186.      * needsInput() or needsDictionary() should be called in order to
  187.      * determine if more input data or a preset dictionary is required.
  188.      * In the later case, getAdler() can be used to get the Adler-32
  189.      * value of the dictionary required.
  190.      * @param b the buffer for the uncompressed data
  191.      * @return the actual number of uncompressed bytes
  192.      * @exception DataFormatException if the compressed data format is invalid
  193.      * @see Inflater#needsInput
  194.      * @see Inflater#needsDictionary
  195.      */
  196.     public int inflate(byte[] b) throws DataFormatException {
  197.     return inflate(b, 0, b.length);
  198.     }
  199.  
  200.     /**
  201.      * Returns the ADLER-32 value of the uncompressed data.
  202.      */
  203.     public synchronized native int getAdler();
  204.  
  205.     /**
  206.      * Returns the total number of bytes input so far.
  207.      */
  208.     public synchronized native int getTotalIn();
  209.  
  210.     /**
  211.      * Returns the total number of bytes output so far.
  212.      */
  213.     public synchronized native int getTotalOut();
  214.  
  215.     /**
  216.      * Resets inflater so that a new set of input data can be processed.
  217.      */
  218.     public synchronized native void reset();
  219.  
  220.     /**
  221.      * Discards unprocessed input and frees internal data.
  222.      */
  223.     public synchronized native void end();
  224.  
  225.     /**
  226.      * Frees the decompressor when garbage is collected.
  227.      */
  228.     protected void finalize() {
  229.     end();
  230.     }
  231.  
  232.     private native void init(boolean nowrap);
  233. }
  234.